package com.struggle.myrabbitmq.sample.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.struggle.myrabbitmq.sample.MyRabbitConstants;
import com.struggle.myrabbitmq.util.ConnectionUtil;
import lombok.SneakyThrows;

/**
 * @className Send
 * @Description 普通模式：rabbitMQ发送消息
 * @Author liuwj3
 * @Date 2019/9/18 0018 下午 10:00
 **/
public class Send {

    @SneakyThrows
    public static void main(String[] args) {
        // 获取到连接及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明（创建）队列
        channel.queueDeclare(MyRabbitConstants.QUEUE_NAME_SIMPLE, false, false, false,null);

        // 发送消息
        String message = "Hello World2!";
        channel.basicPublish("", MyRabbitConstants.QUEUE_NAME_SIMPLE, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        // 关闭通道和连接
        channel.close();
        connection.close();

    }

}
